package com.hjz.hongbaotest.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hjz.hongbaotest.R;
import com.hjz.hongbaotest.utils.Constants;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * ************************************************************
 * 类描述：
 * 作者：Administrator
 * 创建时间：2017-09-25 10:14
 * <p>
 * ************************************************************
 */

public class LoginActivity extends Activity {

    private EditText et_login_name,et_login_pass;
    private Button btn_login,btn_wx_login;
    private IWXAPI api; // IWXAPI 是第三方app和微信通信的openapi接口

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);
        et_login_name= (EditText) findViewById(R.id.et_user_name);
        et_login_pass= (EditText) findViewById(R.id.et_user_password);
        btn_login= (Button) findViewById(R.id.btn_login);
        btn_wx_login= (Button) findViewById(R.id.btn_wx_login);

        initData();
    }

    private void initData() {

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toLogin();
            }
        });

        btn_wx_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toWXLogin();
            }
        });
    }

    private void toWXLogin() {

        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, true);
        api.registerApp(Constants.APP_ID);

        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";//获取个人用户信息的权限
        req.state = "wechat_sdk_demo_test";//防止攻击
        api.sendReq(req);//向微信发送请求

    }

    private void toLogin(){

        final String name=et_login_name.getText().toString().trim();
        String pass=et_login_pass.getText().toString().trim();

        OkGo.<String>post("http://192.168.1.133:8080/redmap/login.shtml")
                .params("loginName",name)
                .params("onePassword",pass)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {


                        try {
                            JSONObject obj = new JSONObject(response.body().toString());
                            String msg = obj.getString("msg");
                            String success=obj.getString("success");

                            if (success.equals("true")){
                                String sessionId=obj.getString("sessionId");
                                SharedPreferences share = getSharedPreferences("user",MODE_PRIVATE);
                                SharedPreferences.Editor edit = share.edit(); //编辑文件
                                edit.putString("name", name);
                                edit.putString("isLogin",success);
                                edit.putString("sessionId",sessionId);
                                edit.commit();  //保存数据信息

                                Toast.makeText(LoginActivity.this,msg,Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(LoginActivity.this,MainActivity.class));
                                finish();
                            }else {
                                Toast.makeText(LoginActivity.this,msg,Toast.LENGTH_SHORT).show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //根据 Tag 取消请求
        OkGo.getInstance().cancelTag(LoginActivity.this);
    }
}
